package org.dlx.drpc.common;

import com.alibaba.fastjson.JSON;
import org.dlx.drpc.core.common.IRPCEncoder;

import java.nio.ByteBuffer;

//默认使用json来编码
public class JsonEncoder implements IRPCEncoder
{
    @Override
    public byte[] encode(Object ob) throws Exception
    {
        byte[] content= JSON.toJSONString(ob).getBytes("UTF8");
        ByteBuffer bb = ByteBuffer.allocate(4 + content.length);
        if(ob instanceof ExpRequest)
            bb.putInt(1);//设置1个type 来区别不同的类型
        else if(ob instanceof ExpResponse)
            bb.putInt(2);
        else
            throw new Exception("不支持的类型");
        bb.put(content);
        return bb.array();
    }
}
